package com.example.yy.zyfypt_223yy.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yy.zyfypt_223yy.R;
import com.example.yy.zyfypt_223yy.bean.LoginBean;
import com.example.yy.zyfypt_223yy.bean.SessionID;
import com.example.yy.zyfypt_223yy.iface.LoginListener;
import com.example.yy.zyfypt_223yy.model.LoginModel;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btn,btn1;
    private EditText idET,pwdEt;
    private TextView tv,et,tv5,tv6;
    private Switch sw;
    private SessionID sessionID;
    private String username="",password="";//用户名密码
    private SharedPreferences sp;//简单存储对象
    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            System.out.println("result:"+loginBean.getId());
            SessionID.SessionID = loginBean.getSessionid();
            SessionID.id = loginBean.getId();
            SessionID.realname = loginBean.getRealname();
            SessionID.rolename = loginBean.getRolename();
            SessionID.username = loginBean.getUsername();
            SessionID.sex = loginBean.getSex();
            if(loginBean.getId()!=null) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            }
        }

        @Override
        public void onFail(String msg) {
            System.out.println("fail");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toast.makeText(this, "登录", Toast.LENGTH_SHORT).show();
        init();
        //获取sharedpreferences对象
        sp=getSharedPreferences("Save",MODE_PRIVATE);
        //读取存储对象
        readSP();
    }

    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        Boolean b=sp.getBoolean("remember",false);
        if(b){
            idET.setText(name);
            pwdEt.setText(pass);
            sw.setChecked(b);
        }
    }

    private void init() {
        btn=(Button)findViewById(R.id.button1);
        btn1=(Button)findViewById(R.id.button2);
        tv=(TextView)findViewById(R.id.username);
        et=(TextView)findViewById(R.id.password);
        tv5=(TextView)findViewById(R.id.textView5);
        tv6=(TextView)findViewById(R.id.textView6);
        idET = (EditText)findViewById(R.id.txt_username);
        pwdEt = (EditText)findViewById(R.id.txt_password);
        sw=(Switch)findViewById(R.id.switch1);

        btn.setOnClickListener(this);
        btn1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button1:
                System.out.println("点击登录按钮！");
                username=idET.getText().toString();
                password=pwdEt.getText().toString();
              /*  LoginModel loginModel = new LoginModel();
                loginModel.getResult(idET.getText().toString(),
                        pwdEt.getText().toString(),
                        loginListener);*/
                LoginModel loginModel = new LoginModel();
                loginModel.getResult(idET.getText().toString(),pwdEt.getText().toString(),loginListener);
                saveSP();//存储信息
                break;
            case R.id.button2:
                System.out.println("点击注册按钮！");
                Intent intent = new Intent();
                intent.setClass(this, RegisterActivity.class);
                startActivity(intent);
                break;

        }
    }

    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();//提交存储
    }
}

